#############################################################
## Replication file for Ban, Palmer and Schneer 2019       ##
## file: epu_plot.R                                        ##
## date: 5/16/2019                                         ##
#############################################################

#install relevant R packages
#install.packages(c('foreign','readstata13','data.table'))

rm(list=ls())
library(foreign)
library(readstata13)
library(data.table)

#set working directory
setwd("/nfs/home/B/bschneer/shared_space/bschneer/lobbying_replication")

#import EPU data

data_agg <- read.dta("inputs/EPU_vars.dta")
data_cat <- fread("inputs/Categorical_EPU_Data.csv")

#Identify sub-categories
data_cat2 <- data_cat[,c(7,8,9,10),with=F]

#Take average of sub-categories
data_cat[,policy_index:=rowMeans(data_cat2)]

#Create Date variable
data_cat[,Date:=as.Date(Date,"%m/%d/%y")]

#Create year variable
data_cat[,year:=year(Date)]

#Get averages over time
epu <- data_cat[year %in% 2000:2014,.(epu=mean(get('1. Economic Policy Uncertainty')),pu=mean(policy_index)),by=year]

#Output plot over time
pdf ("out/EPU_2000_2014.pdf",
     width = 12.00, height=8.00)


plot(data_agg$year[data_agg$year>=2000 & data_agg$year<=2014], data_agg$news_epu_year[data_agg$year>=2000 & data_agg$year<=2014], type='o', ylim= c(40,max(epu$epu,epu$pu)),
 xlab="Year", ylab="Economic Policy Uncertainty",
     panel.first = {
       usr <- par('usr')
       rect(c(2001,2008), usr[3], c(2003,2013), usr[4], col='lightblue', border=NA)
     },
)

lines(x=epu$year,y=epu$pu,type='o',lty=3)
legend("topleft",lty=c(1,3),c("EPU","Policy Focused"),bty="n")

graphics.off()




